<script setup lang="ts">
import { computed, inject, onMounted, ref, watch } from 'vue'


const screenSizeLevel = inject('screenSizeLevel')
let temp = 0
let flag = false
let leftItemList = ref()
let item = ref()
let leftArrow = ref()
let rightArrow = ref()
const bannerLeft = ref()
let itemList = computed({
  get(){
    return leftItemList.value.querySelectorAll('.slideshow__item')
  },
  set(v){
  }
})


function invertflag() {
  flag = !flag
}

watch(screenSizeLevel, (v, old) => {
  if (v === 1) {
    bannerLeft.value.style = 'grid-column: 1 / -1'
  } else if (v === 2) {
    bannerLeft.value.style = 'grid-column: 1 / -1'
  } else {
    bannerLeft.value.style = 'grid-column: 1 / 8'
  }
})

function onLeftArrowClick() {
  console.log(flag)
  if (!flag) {
    invertflag()
    console.log(flag)
    if (temp == 0) {
      temp = itemList.value.length - 1
      leftItemList.value.style.right = temp * item.value.offsetWidth + 'px'
    } else {
      leftItemList.value.style.right = (--temp) * item.value.offsetWidth + 'px'
    }
    window.setTimeout(invertflag, 1200)
  }

}

function onRightArrowClick() {
  if (!flag) {
    invertflag()
    if (temp == itemList.value.length - 1) {
      leftItemList.value.style.right = 0 + 'px'
      temp = 0
    } else {
      leftItemList.value.style.right = (++temp) * item.value.offsetWidth + 'px'
    }
    setTimeout(invertflag, 1200)
  }
}

onMounted(() => {
  window.addEventListener('resize', function() {
    leftItemList.value.style.right = temp * item.value.offsetWidth + 'px'
  })
  setInterval(()=>{
    let event = new Event('click')
    if(rightArrow.value){
      rightArrow.value.dispatchEvent(event)
    }
  }, 3000)
})


</script>

<template>
  <div class="banner">
    <div class="banner__left" ref="bannerLeft" style="grid-column: 1 / 8">
      <div id="slideshow" class="slideshow">
        <div class="slideshow__arrow">
          <div class="arrow-block">
            <div class="slideshow__arrow-left" id="slideshow__arrow-left" ref="leftArrow"
                 @click="onLeftArrowClick"></div>
            <div class="slideshow__arrow-right" id="slideshow__arrow-right" ref="rightArrow"
                 @click="onRightArrowClick"></div>
          </div>
        </div>
        <div class="slideshow__item__list" style="right:0;" ref="leftItemList">
          <div class="slideshow__item" ref="item"></div>
          <div class="slideshow__item"></div>
          <div class="slideshow__item" ></div>
          <div class="slideshow__item" ></div>
          <div class="slideshow__item" ></div>
        </div>
      </div>
    </div>
    <!--end of banner-left-->
    <div class="banner__right" v-if="screenSizeLevel ===3">
      <ul class="banner__list">
        <li class="banner__list-unit"></li>
        <li class="banner__list-unit"></li>
        <li class="banner__list-unit"></li>
        <li class="banner__list-unit"></li>
      </ul>
    </div>
    <!--end of banner-->
  </div>
</template>

<style scoped lang="scss">

.banner {

  //height: 700px;
  display: grid;
  grid-template-columns: subgrid;
  overflow: hidden;

  .banner__left {
    //grid-column: 1 / 8;
    background-color: #706eff;
    padding: var(--spacing-xs);
  }

  .banner__right {
    grid-column: 8 / -1;
    background-color: #9cade5;
    //padding: var(--spacing-m);

    padding: var(--spacing-s);

    .banner__list {
      display: grid;
      grid-template-rows: repeat(4, 1fr);
      grid-auto-rows: min-content;
      grid-row-gap: var(--spacing-s);
      padding: 0;
      overflow: scroll;
      height: 100%;

      .banner__list-unit {
        list-style: none;
        height: 150px;
      }
    }
  }

  .slideshow {
    position: relative;
    height: 100%;
    border-radius: 36px 12px 12px 12px;
    overflow: hidden;

    .slideshow__arrow {
      position: absolute;
      width: 100%;
      height: 100%;
      z-index: 1;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .arrow-block {
        width: 100%;
        height: 120px;
        position: relative;

        .slideshow__arrow-left {
          width: 10%;
          height: 100%;
          float: left;
          opacity: 0;
          background-image: url(/left.png);
          background-size: contain;
          background-repeat: no-repeat;
          background-position: left;
          transform: rotate(180deg) translate(100%);
          transition: all 1s cubic-bezier(.1, 1, 0.5, 1);
        }

        .slideshow__arrow-right {
          width: 10%;
          height: 100%;
          float: right;
          opacity: 0;
          background-image: url(/left.png);
          background-size: contain;
          background-repeat: no-repeat;
          background-position: left;
          transform: translate(100%);
          transition: all 1s cubic-bezier(.1, 1, 0.5, 1);

        }
      }

    }

    .slideshow__item__list {
      position: relative;
      height: 100%;
      width: 500%; // need v-bind
      display: flex;
      flex-wrap: nowrap;
      transition: all 1s cubic-bezier(0.1, 1, 0.5, 1);
      .slideshow__item {
        height: 100%;
        width: 100%;
        background-position: center;
      }

      .slideshow__item:nth-child(1) {
        background: url(/BannerImage/T337.JPG);
        background-size: cover;
      }

      .slideshow__item:nth-child(2) {
        background: url(/BannerImage/ACG.GY_23.jpg);
        background-size: cover;
      }

      .slideshow__item:nth-child(3) {
        background: top url(/BannerImage/T266.jpg);
        background-size: cover;
      }

      .slideshow__item:nth-child(4) {
        background: center url(/BannerImage/083.jpg);
        background-size: cover;
      }
      .slideshow__item:nth-child(5){
        background:center url(/BannerImage/ACG.GY_36.jpg);
        background-size: cover;
      }
    }
  }

}

#slideshow:hover .slideshow__arrow-right {
  transform: translate(20%);
  opacity: 1;
  transition: all 1s cubic-bezier(0.1, 1, 0.5, 1);
}

#slideshow:hover .slideshow__arrow-left {
  transform: rotate(180deg) translate(20%);
  opacity: 1;
  transition: all 1s cubic-bezier(0.1, 1, 0.5, 1);
}
</style>